using UnityEngine;
using Yoozoo.Gameplay.RTS.Proto;

namespace Yoozoo.Gameplay.RTS
{
    public class SimpleHud : HudBase
    {
        public GameObject MapPoliceHpBarLod0Obj;
        private MapPoliceHpBar MapPoliceHpBar;
        private bool objLodActive = false;
        private bool isInBattle = false;
        public override void Initialize(int team)
        {
            var hudManager = ArmyManager.Instance.HudManager;
            MapPoliceHpBarLod0Obj = hudManager.GetHud(HudManager.MapPoliceHpBarLod0);
            if (MapPoliceHpBarLod0Obj!=null)
            {
                MapPoliceHpBar = MapPoliceHpBarLod0Obj.GetComponent<MapPoliceHpBar>();
                objLodActive = false;
                MapPoliceHpBar.transform.localScale = Vector3.zero;
                MapPoliceHpBar.transform.SetParent(MarchEntity.Transform);
                MapPoliceHpBar.transform.localRotation = Quaternion.Euler(45,45,0);
                MapPoliceHpBar.transform.localPosition = Vector3.zero;
            }
        }
        protected override void SelfInit()
        {
        }

        protected override void InitParentScaleAndPosition()
        {
        }

        public override void SetSelected(bool selected, bool isMultySelect)
        {
        }

        public override void UpdatePlayerName()
        {
        }

        protected override void InitCommanderHead()
        {
        }

        protected override void InitSoldierNumAndRage()
        {
        }

        public override void SetTeam(int team)
        {
        }

        public override void OnCameraPosUpdated(MarchContext marchContext)
        {
        }

        protected override void InitDefaultTrigger()
        {
            
        }

        public override void RoundFight(int soldierCurrentCount, int initTotalCount,bool isInBattle)
        {
            this.isInBattle = isInBattle;
            MarchEntity.MarchInfo.SetIsInBattle(isInBattle);
            if (isInBattle)
            {
                if (!objLodActive)
                {
                    SetHudLodScale(LodHudType.PoliceHpBar, MapPoliceHpBarLod0Obj, MarchManager.Instance.marchContext);
                    MapPoliceHpBarLod0Obj.SetActive(true);
                    objLodActive = true;
                    ArmyManager.Instance.GetRtsResourceManager().LoadPoliceSeigeEffect(GetSeigeCallBack);
                }
                MapPoliceHpBar.SetValue((float)soldierCurrentCount / initTotalCount);
            }
            else
            {
                if (objLodActive)
                {
                    objLodActive = false;
                    MapPoliceHpBarLod0Obj.transform.localScale = Vector3.zero;
                    DestroyEffect();
                }
            }
        }

        private GameObject effect;
        private void GetSeigeCallBack(GameObject obj)
        {
            if (isInBattle)
            {
                effect = obj;
                effect.transform.position = MarchEntity.MarchInfo.Position;
            }
            else
            {
                DestroyEffect();
            }
           
        }

        private void DestroyEffect()
        {
            if (effect!=null)
            {
                GameObject.Destroy(effect);
                effect = null;
            }
        }

        protected override void OnUpdateLowLod(MarchContext marchContext)
        {
            base.OnUpdateLowLod(marchContext);
            SetLod0Visible(marchContext);
        }
        
        private void SetLod0Visible(MarchContext marchContext)
        {
            bool isShowLod0 = marchContext.LodLevel < WorldEntry.MaxShowMarchLodLevel;
            bool lod0Visible = isShowLod0 && isInBattle;
            if (objLodActive != lod0Visible)
            {
                if (!lod0Visible)
                {
                    MapPoliceHpBarLod0Obj.transform.localScale = Vector3.zero;
                    DestroyEffect();
                }
                else
                {
                    base.SetHudLodScale(LodHudType.PoliceHpBar, MapPoliceHpBarLod0Obj, marchContext);
                }
                objLodActive = lod0Visible;
                if (objLodActive)
                {
                    ArmyManager.Instance.GetRtsResourceManager().LoadPoliceSeigeEffect(GetSeigeCallBack);
                }
            }
        }

        protected override void OnLod1ScaleChanged(MarchContext marchContext)
        {
        }

        protected override void OnLod0ScaleChanged(MarchContext marchContext)
        {
            if (MapPoliceHpBarLod0Obj!=null && isInBattle)
            {
                SetHudLodScale(LodHudType.PoliceHpBar, MapPoliceHpBarLod0Obj, marchContext);
            }
        }

        public override void ForceShowSelectedUI(bool isShow, bool isBattleStyle)
        {
            
        }

//        public void SetEntityAndTroopData(MarchEntity marchEntity, MapPoliceOffice monster)
//        {
//            MarchEntity = marchEntity;
//        }
        public override void Dispose()
        {
            base.Dispose();
            var hudManager = ArmyManager.Instance.HudManager;
            if (MapPoliceHpBarLod0Obj!=null)
            {
                hudManager.RecoverHud(HudManager.MapPoliceHpBarLod0,MapPoliceHpBarLod0Obj);
                MapPoliceHpBarLod0Obj = null;
                MapPoliceHpBar = null;
            }

            objLodActive = false;
            DestroyEffect();
        }
    }
}